#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QMessageBox"

#include "form_boardpara.h"
#include "form_cachedata.h"
#include "form_programwashpara.h"

SerialPortUsr * g_serial_port_pt;

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    g_serial_port_pt = new SerialPortUsr;
    QStringList serial_string_list = g_serial_port_pt->scanSerial();
    for (int kI = 0;kI < serial_string_list.size() ; kI ++)
    {
        ui->comboBox_portNum->addItem(serial_string_list[kI]);
    }
    ui->comboBox_baud->addItem("9600");
    ui->comboBox_baud->addItem("115200");
    ui->comboBox_baud->setCurrentText("115200");
    connect(g_serial_port_pt,SIGNAL(readSignal()),this,SLOT(read_serial_data()));//当串口接收到数据时，该槽就会响应。

    label_status_pt = new QLabel(this);
    label_status_pt->setText("com closed");
    ui->statusbar->addPermanentWidget(label_status_pt);

    ui->textEdit_receive->setReadOnly(true);

    menuBar_pt = ui->menubar;
    connect(menuBar_pt,SIGNAL(triggered(QAction * )),this,SLOT(menubar_trigged_reply(QAction * )));
}

MainWindow::~MainWindow()
{
    delete ui;
}

/**
  * @brief  打开按钮单击响应槽函数
  * @details 
  * @param  无
  * @retval 无
  */
void MainWindow::on_pushButton_open_clicked()
{
    if(g_serial_port_pt->open(ui->comboBox_portNum->currentText(), ui->comboBox_baud->currentText().toInt()))
    {
        // 关闭下拉列表使能
        ui->comboBox_portNum->setEnabled(false);
        ui->comboBox_baud->setEnabled(false);
        // 关闭打开按钮
        ui->pushButton_open->setEnabled(false);
        label_status_pt->setText("com opened");
    }
    else
    {
        QMessageBox::information(NULL,"warnning","open serial port faild!");
    }

}

/**
 * @brief 关闭按钮单击响应槽函数
 * @details
 * @param none
 * @return void
 */
void MainWindow::on_pushButton_close_clicked()
{
    // 关闭串口
    g_serial_port_pt->close();
    // 重新开启下拉列表使能
    ui->comboBox_portNum->setEnabled(true);
    ui->comboBox_baud->setEnabled(true);
    // 使能打开按钮
    ui->pushButton_open->setEnabled(true);
    label_status_pt->setText("com closed");
}

/**
  * @brief  读取串口数据响应槽函数
  * @details 该函数与SerialPortUsr类的串口接收完成信号相关联。
  * @param  无
  * @retval 无
  */
void MainWindow::read_serial_data()
{

    QString originStr = ui->textEdit_receive->toPlainText(); // 保存接收文本框原本的数据
    ui->textEdit_receive->clear();
    ui->textEdit_receive->setText(originStr);
    ui->textEdit_receive->moveCursor(QTextCursor::End); // 在末尾移动光标一格
    if(ui->checkBox_hex->isChecked())
      ui->textEdit_receive->insertPlainText(g_serial_port_pt->getReadBuf().toHex(' ')); // 插入新读取数据（中间以一格空格间隔）
    else
      ui->textEdit_receive->insertPlainText(g_serial_port_pt->getReadBuf());
    ui->textEdit_receive->moveCursor(QTextCursor::End); // 在末尾移动光标一格
    if(ui->checkBox_hex->isChecked())
      ui->textEdit_receive->insertPlainText(" ");


    g_serial_port_pt->clearReadBuf(); // 读取完后，清空数据缓冲区
}

/**
 * @brief 发送按钮单击响应
 * @param
 * @retval
 */
void MainWindow::on_pushButton_send_clicked()
{
    QByteArray sendData = g_serial_port_pt->hexStringToByteArray(ui->textEdit_send->toPlainText());
    g_serial_port_pt->sendData(sendData);
}

/**
 * @brief 菜单栏响应程序
 * @param act_pt : 响应的控件
 * @retval
 */
void MainWindow::menubar_trigged_reply(QAction * act_pt)
{
    if(act_pt->text() == tr("print para"))
    {
        QByteArray temp_arr = g_serial_port_pt->hexStringToByteArray("EE A1 F2 00 03 00 01 30 00 FF FC FF FF");
        g_serial_port_pt->sendData(temp_arr);
        qDebug()<<"print para";
    }
    if(act_pt->text() == tr("microplate washer"))
        qDebug()<<"press microplate washer";
    if(act_pt->text() == tr("wash pipeline"))
    {
        QByteArray temp_arr = g_serial_port_pt->hexStringToByteArray("EE A1 03 00 03 00 05 30 00 FF FC FF FF");
        g_serial_port_pt->sendData(temp_arr);
        qDebug()<<"wash pipeline";
    }
    if(act_pt->text() == tr("wash plate"))
    {
        QByteArray temp_arr = g_serial_port_pt->hexStringToByteArray("EE A1 02 00 01 00 31 30 00 FF FC FF FF");
        g_serial_port_pt->sendData(temp_arr);
        qDebug()<<"wash plate";
    }
    if(act_pt->text() == tr("board para"))
    {
        Form_BoardPara * form_pt = new Form_BoardPara();
        form_pt->show();
    }
    if(act_pt->text() == tr("cache data"))
    {
        Form_CacheData * form_pt = new Form_CacheData();
        form_pt->show();
    }
    if(act_pt->text() == tr("program wash para"))
    {
       Form_ProgramWashPara * form_pt = new Form_ProgramWashPara();
       form_pt->show();
    }
    if(act_pt->text() == tr("open or close door"))
    {
        QByteArray temp_arr = g_serial_port_pt->hexStringToByteArray("EE A1 01 00 01 00 06 30 00 FF FC FE FF");
        g_serial_port_pt->sendData(temp_arr);
        qDebug()<<"open or close door";
    }
}

void MainWindow::on_pushButton_clean_clicked()
{
    ui->textEdit_receive->clear();
}
